package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.RoleMapper;
import com.heima.mapper.UserMapper;
import com.heima.pojo.entity.Node;
import com.heima.pojo.entity.Role;
import com.heima.pojo.entity.User;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.pojo.vo.PageVo;
import com.heima.pojo.vo.UserPageVo;
import com.heima.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public LoginUserVo login(LoginDto dto) {

        if(dto == null){
            //参数异常
        }

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName,dto.getLoginName());
        User user = userMapper.selectOne(wrapper);

        if(user == null){ //代表用户不存在得

        }
        if(!user.getPassword().equals(dto.getPassword())){ //代表密码错误

        }
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(1111);
        return loginUserVo;
    }

    /**
     * 人员搜索 条件分页查询
     * @param pageIndex
     * @param pageSize
     * @param userName
     * @return
     */
    @Override
    public PageVo<UserPageVo> getUserList(Integer pageIndex, Integer pageSize, String userName) {
        PageVo<UserPageVo> userPage = new PageVo<>();
        //进行分页和查询 用户表
        PageHelper.startPage(pageIndex,pageSize);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(userName!=null,User::getUserName,userName);
        List<User> users = userMapper.selectList(queryWrapper);
        //将查询结果强转为page 并封装要返回的数据
        Page<User> page = (Page<User>) users;
        userPage.setPageIndex(pageIndex);
        userPage.setPageSize(pageSize);
        userPage.setTotalCount(page.getTotal());
        userPage.setTotalPage(page.getPages());

        List<UserPageVo> userPageVoList = new ArrayList<>();
        //获取currentPageRecords中的对象userPageVoList
        for (User user : users) {
            UserPageVo userPageVo = new UserPageVo();
            BeanUtils.copyProperties(user,userPageVo);
            //查询role内容
            LambdaQueryWrapper<Role> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(user.getRoleId()!=null,Role::getRoleId,user.getRoleId());
            Role role = roleMapper.selectOne(queryWrapper2);
            userPageVo.setRole(role);
            //将对象添加到集合中去
            userPageVoList.add(userPageVo);
        }
        //把集合放入返回对象的currentPageRecords中
        userPage.setCurrentPageRecords(userPageVoList);
        return userPage;
    }

    /**
     * 修改用户信息
     * @param userId
     * @param user
     * @return
     */
    @Override
    public Boolean update(Long userId, User user) {
       Boolean result =  userMapper.update(userId,user);
        return result;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public Boolean delete(Long id) {
        int i = userMapper.deleteById(id);
        if (i==1){
            return true;
        }
        return false;
    }
}
